package com.hua.common.photopicker.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.hua.common.BaseActivity;
import com.hua.common.R;
import com.hua.common.photopicker.util.Photo;
import com.hua.common.photopicker.util.PhotoDirectory;

import java.util.List;


public class PhotoGridActivity extends BaseActivity implements AdapterView.OnItemClickListener {

	private GridView gridView=null;
	private List<Photo> photoList=null;
	private boolean isForResoult=false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.initData();
		this.initViews();
		this.setPageTitle(getIntent().getStringExtra("name") + "");
		this.isForResoult=getIntent().getBooleanExtra("isforresoult",false);
	}

	/**
	 * 初始化数据
	 */
	private void initData(){
		PhotoDirectory photoDirectory=(PhotoDirectory) getIntent().getSerializableExtra("data");
		if(photoDirectory!=null){
			photoList=photoDirectory.getPhotos();
		}
	}
	/**
	 * 初始化界面
	 */
	private void initViews(){
		this.setContentViewItem(R.layout.photogrid);
		gridView=(GridView) this.findViewById(R.id.gridview);
		gridView.setOnItemClickListener(this);
		gridView.setAdapter(new PhotoGridAdapter(this, photoList));
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Photo photo= (Photo) parent.getAdapter().getItem(position);
		if(this.isForResoult){
			Intent intent=new Intent();
			intent.putExtra("path", ""+photo.getPath());
			setResult(0, intent);
			this.finish();
		}else {
			Intent it =new Intent(Intent.ACTION_VIEW);
			it.setDataAndType(Uri.parse("file://" + photo.getPath()), "image/*");
			startActivity(it);
		}

	}
}
